﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Net;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using IDotNet.MapLibs.Web.Model;
using Newtonsoft.Json;

namespace IDotNet.MapLibs.Web
{
    // 注意: 使用“重构”菜单上的“重命名”命令，可以同时更改代码、svc 和配置文件中的类名“MapService”。
    // 注意: 为了启动 WCF 测试客户端以测试此服务，请在解决方案资源管理器中选择 MapService.svc 或 MapService.svc.cs，然后开始调试。
    public class MapService : IMapService
    {
        public AddressLocationResultInfo GetAddressLocation(string address, string city)
        {
            var param = new Dictionary<string, string>()
            {
                {"city",city.UrlEncode()},
                {"output","json"},
                {"address",address.UrlEncode()},
                {"ak",CommFunc.GetAppSettingValue("BMap.Ak")},
            };
            var url = CommFunc.GetAppSettingValue("BaiDuGeocodingUri");
            var paramData = string.Join("&", param.Select(p => p.Key + "=" + p.Value));
            using (var webClient = new WebClient())
            {
                var data = webClient.DownloadString(url + "?" + paramData);
                var res = JsonConvert.DeserializeObject<BMapGeocoderResultModel>(data);
                if (res.IsSuceess())
                {
                    return new AddressLocationResultInfo
                    {
                        IsSuccess = true,
                        PointModel = res.AddressLocaltion
                    };
                }
                return new AddressLocationResultInfo
                {
                    ErrorMsg = res.Msg
                };
            }
        }

        public List<MapPointDistanceModel> GetDistance(List<MapPointModel> data, PointModel curPoint)
        {
            var bMapLib = new BMapLib();
            return data.Select(p => new MapPointDistanceModel
            {
                PointModel = p.PointModel,
                Id = p.Id,
                Distance = bMapLib.GetDistance(p.PointModel, curPoint)
            }).ToList();
        }

        public List<MapPointDistanceModel> GetDistnaceNearby(List<MapPointModel> data, PointModel curPoint, int top)
        {
            var bMapLib = new BMapLib();
            return data.Select(p => new MapPointDistanceModel
            {
                PointModel = p.PointModel,
                Id = p.Id,
                Distance = bMapLib.GetDistance(p.PointModel, curPoint)
            }).OrderBy(p => p.Distance).Take(top).ToList();
        }


        public bool IsInsidePolygon(PointModel p, List<PointModel> pts)
        {
            var bMapLib = new BMapLib();
            return bMapLib.IsInsidePolygon(p, pts);
        }


        public Model.MapAreaSearchResult IsInsidePolygon(PointModel p, List<Model.MapData> pts)
        {
            var bMapLib = new BMapLib();
            foreach (var mapData in pts)
            {
                foreach (var data in mapData.datas)
                {
                    if (bMapLib.IsInsidePolygon(p, data.paths))
                    {
                        return new MapAreaSearchResult()
                        {
                            mapId = mapData.mapId,
                            data = data
                        };
                    }
                }
            }
            return null;
        }
    }
}
